//
//  ZJDiscussContactsViewController.m
//  ShangHaiHuDongApp
//
//  Created by ZJ on 2017/9/4.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "ZJDiscussContactsViewController.h"
#import "ZJDCTableViewCell.h"
#import "LPJoinPersonListModel.h"
#define CELLID  @"ZJDiscussContactsViewController"
@interface ZJDiscussContactsViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    NSInteger _page;
}
/** 表 */
@property (nonatomic, strong) UITableView *tableView;
/** 数据 */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** 滑动 */
@property (nonatomic, strong) ZJRefurbish *refurbish;

@end

@implementation ZJDiscussContactsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = @"聊天室成员";
    [self.view addSubview:self.tableView];
    
    __weak typeof(self) weakSelf = self;
    [self.refurbish headerToScollview:self.tableView Block:^{
        _page = 1;
        [weakSelf netWorking_header];
        
    }];
    [self.refurbish footerToScollview:self.tableView Block:^{
        _page++;
        [weakSelf netWorking_footer];
    }];
    
    [self.tableView.mj_header beginRefreshing];

}

#pragma mark - 数据

//下拉刷新
- (void)netWorking_header{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"AI_ID"] = self.ID;
    paramer[@"PAGE"] = page;
    paramer[@"COUNT"] = @"20";
    paramer[@"STATUS"] = @"0";
    
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActEnrollList] Parameter:paramer Success:^(id object) {
        NetWorkModel *model = [NetWorkModel yy_modelWithJSON:object];
        if ([model.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ENROLL_SIGN_LEAVE"];
            weakSelf.dataArray = [[NSArray yy_modelArrayWithClass:[LPJoinPersonListModel class] json:array] mutableCopy];
            
        }else{
            [weakSelf.dataArray removeAllObjects];
        }
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataArray.count == 0) {
            [LYEmptyManager showNoDataWithView:weakSelf.tableView];
        }
    } Fail:^(id object) {
        [weakSelf.dataArray removeAllObjects];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
        if (weakSelf.dataArray.count == 0) {
            [LYEmptyManager showLoadFailWithView:weakSelf.tableView managerBlock:^{
                [weakSelf.tableView.mj_header beginRefreshing];
            }];
        }else{
            [YJProgressHUD showMsgWithoutView:@"请求失败"];
        }
    }];
    
}

//上拉加载
- (void)netWorking_footer{
    __weak typeof(self) weakSelf = self;
    NSString *page = [NSString stringWithFormat: @"%ld",(long)_page];
    NSMutableDictionary *paramer = [NSMutableDictionary dictionary];
    paramer[@"AI_ID"] = self.ID;
    paramer[@"PAGE"] = page;
    paramer[@"COUNT"] = @"20";
    paramer[@"STATUS"] = @"0";
    [NetWorkingManager postWithUrlString:[NetWorkingURL getactmactionActEnrollList] Parameter:paramer Success:^(id object) {
        
        NetWorkModel *mode = [NetWorkModel yy_modelWithJSON:object];
        if ([mode.STATE isEqualToString:@"20000"]) {
            NSArray *array = [object objectForKey:@"ENROLL_SIGN_LEAVE"];
            [weakSelf.dataArray addObjectsFromArray:[NSArray yy_modelArrayWithClass:[LPJoinPersonListModel class] json:array]];
            [weakSelf.tableView reloadData];
        }
        [weakSelf.tableView.mj_footer endRefreshing];
        
    } Fail:^(id object) {
        [weakSelf.tableView.mj_footer endRefreshing];
        
    }];
    
}


#pragma mark - UITableView 协议方法
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZJDCTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CELLID forIndexPath:indexPath];
    cell.model = self.dataArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60 * [UIScreen zj_screenHScale];
}
#pragma mark - Setter


#pragma mark - Getters
- (UITableView *)tableView{
    if (_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, LP_TopHeight, self.view.width, self.view.height - LP_TopHeight) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZJDCTableViewCell class] forCellReuseIdentifier:CELLID];
    }
    return _tableView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil){
        _dataArray = [NSMutableArray array];
        
    }
    return _dataArray;
}
- (ZJRefurbish *)refurbish{
    if (!_refurbish) {
        _refurbish = [ZJRefurbish new];
    }
    return _refurbish;
}

@end
